//index.js
//获取应用实例
const app = getApp();
const innerAudioContext = wx.createInnerAudioContext();
var timer;
Page({
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '你有一份恶搞神器等待领取',
      path: '/pages/index/index',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  data: {
    icon_url: '../../images/pause.png',
    is_work: false
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    innerAudioContext.autoplay = false;
    innerAudioContext.loop = true;
    innerAudioContext.src = 'http://music-1253263847.coscd.myqcloud.com/s3_active.mp3';
    innerAudioContext.onError((res) => {
      console.log(res.errMsg);
      console.log(res.errCode);
      if (innerAudioContext !== null){
        innerAudioContext.stop();
      }
      
    });
  },
  onHide : function() {
    this.setData({
      is_work: false,
      icon_url: '../../images/pause.png',
    });
    clearTimeout(timer);
  },
  start: function () {
    if (this.data.is_work == false){
      this.setData({
        is_work: true,
        icon_url: '../../images/work.png',
      });
      zhengdong();
      innerAudioContext.play();
      innerAudioContext.onPlay(() => {
        //console.log('开始播放')
      });
    } else {
      this.setData({
        is_work: false,
        icon_url: '../../images/pause.png',
      });
      innerAudioContext.pause();
      clearTimeout(timer);
      innerAudioContext.onPause(() => {
        //console.log('暂停播放')
      });
    }
  },
});
function zhengdong() {
  timer = setTimeout(function () {
    wx.vibrateLong();
    zhengdong();
  }, 400);
}
